package com.wise.service.cms.content.uploadresource;

import com.wise.entities.content.Resource;
import com.wise.entities.content.ResourceGroup;
import com.wise.framework.daomodel.DaoIncrement;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoRemove;
import com.wise.framework.daomodel.IncrementUpdate;
import com.wise.framework.validation.Precondition;
import com.wise.summer.v1.cms.content.uploadresource.DeleteUploadResource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;

import static com.wise.model.content.ContentErrMsgs.Err_100100000004;

/**
 * @author yubo
 */
@Service
@SummerService
public class DeleteUploadResourceService implements SummerServiceBean<DeleteUploadResource> {
//    @Autowired
//    private OssClientService ossClientService;

    @Override
    public void sum(DeleteUploadResource summer) {
        Precondition.checkNotNull(summer.getList(), Err_100100000004);
        int size = summer.getList().size();
//        List<String> forDeleteFileOssKey = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            Resource resource = DaoOne4Update.s(Resource.class, summer.getList().get(i), Err_100100000004);

            Precondition.checkNotNull(resource, Err_100100000004);
//            forDeleteFileOssKey.add(resource.getOssId());
            DaoRemove.s(resource);

            IncrementUpdate inc = new IncrementUpdate();
            inc.setTableByClass(ResourceGroup.class);
            inc.setId(resource.getId());
            inc.setExtCondation(ResourceGroup._count + ">=1");
            inc.addIncValue(ResourceGroup._count, "-1");
            DaoIncrement.s(inc);
        }
        //不删除oss上的文件
        //delete(forDeleteFileOssKey);
        summer.setSummerResult(true);
    }

    /**
     * OSS删除
     *
     * @param list
     */
    private void delete(List<String> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            for (String s : list) {
//                ossClientService.removeForOss(s);
            }
        }
    }

//    public static void main(String[] args) {
//        OSS oss2 = new OSSClient(
//                "oss-cn-shanghai.aliyuncs.com",
//                "Qx1pJWvQTpF1Aldb",
//                "F77bMtYop1rgDI2KqdLxL4FIxdEGUP");
//        oss2.putObject("pic-wise-com", "fTestk1", new File("/Users/futao/Desktop/壁纸/2.jpg"));
//        oss2.deleteObject("pic-wise-com", "fTestk1");
//
//
//        System.out.println(StringUtils.repeat("---", 20));
//
//        OSS oss = new OSSClient(
//                "oss-cn-shanghai.aliyuncs.com",
//                "LTAICbJAg6SzFwZZ",
//                "O71EjShJr0J70Pd126rT25beY9pXAJ");
//        oss.putObject("dt-app-dev-bucket1", "fTestk1", new File("/Users/futao/Desktop/壁纸/2.jpg"));
//        oss.deleteObject("dt-app-dev-bucket1", "fTestk1");
//    }
}
